"use strict"
var db = require('../db-connection');
const Login = require('../Models/login');

class loginDB {
        updateUserPassword(req,res){
            var sql = `update login set password = '${req.body.password}' , emailaddress='${req.body.emailaddress}'  where login_id = '${req.body.id}'`
            // var sql = "UPDATE node_user SET name = ?,age = ? WHERE id = ?"
            db.query(sql,function(err,result){
                if(!err){
                    res.json({status:"1",message:"密码修改成功"})
                }
            })
        }
        getLoginCredentials(request, respond){
           var emailaddress = request.body.emailaddress;
           var password = request.body.password;
           var msg = "";
    
           var sql = "SELECT password,lastname,login_id FROM areyouhungry.login WHERE emailaddress = ?";
    
            db.query(sql, [emailaddress], function (error, result) {
                if(error){
                    throw error;
                }
                else{
                    if(result.length > 0){
                        // 现在查出来的result 里面不是只有密码吗  把 其他的用户信息也查出来
                        if(password == result[0].password){
                            msg = "SUCCESS!";
                            console.log(msg);

                            respond.json({status:1,message:"登录成功",userInfo:result[0]})
                        }
                        else{
                            msg = "FAIL!";
                            console.log(msg);
                            respond.json({status:0})
                        }
                    }
                    else{
                        msg = "USER NOT FOUND!";
                            console.log(msg);
                        respond.json({status:0})
                    }
                    // respond.json(prepareMessage(msg));
                }
              });
            }

    getAllLogin(request, respond){
        var sql = "SELECT * FROM areyouhungry.login";
        db.query(sql, function(error, result){
            if(error){
                throw error;
            }
            else{
                respond.json(result);
            }
        });
    }

    addLogin(request, respond){
        var loginObject = new Login(null, request.body.firstname , request.body.lastname , request.body.password , request.body.emailaddress , request.body.gender );
        var sql = "INSERT INTO areyouhungry.login (firstname, lastname, password, emailaddress, gender) VALUES(?,?,?,?,?)";        
        var values = [loginObject.getFirstname(), loginObject.getLastname(), loginObject.getPassword(), loginObject.getEmailaddress(), loginObject.getGender()];
       
        db.query(sql, values, function (error, result) {
                if(error){
                    throw error;
                }
                else{
                    respond.json(result);
                }
              });
        }

    updateLogin(request, respond){            
        var loginObject = new Login(request.params.id, request.body.firstname, request.body.lastname, request.body.password, request.body.emailaddress, request.body.gender);

        var sql = "UPDATE areyouhungry.login SET emailaddress = ?, password = ? WHERE login_id = ?";
        var values = [loginObject.getEmailaddress(), loginObject.getPassword(), loginObject.getLogin_id()];
        db.query(sql, values, function (error, result) {
                if(error){
                    throw error;
                }
                else{
                    respond.json(result);
                }
              });
        }

    deletelogin(request, respond){
        var loginID = request.params.id;
        var sql = "DELETE FROM areyouhungry.login WHERE login_id = ?";
        db.query(sql, loginID, function (error, result) {
            if(error){
                throw error;
            }
            else{
                respond.json(result);
            }
          });
    }
}
    function prepareMessage(msg){
        var obj = {"message": msg};
        return obj;

}

module.exports = loginDB;